#!/usr/bin/env python
# -*- coding: utf-8 -*-
#$Id$

from PyQt4 import QtCore, QtGui

from sys import path
path.append('..')
import sigui_rc
import cli_page_ui
import gui_page_ui



class SettingsPage(QtGui.QWidget, cli_page_ui.Ui_CliSettingsPage):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setupUi(self)
		self._cbDelNews.setChecked(False)

class GUIPage(QtGui.QWidget, gui_page_ui.Ui_GUISettingsPage):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setupUi(self)


class SetupDialog(QtGui.QDialog):
	def __init__(self, parent=None):
		QtGui.QDialog.__init__(self, parent)

		self.mnu = QtGui.QListWidget()
		self.pages = QtGui.QStackedWidget()

		# icon-bar menu
		self.mnu.setViewMode(QtGui.QListView.IconMode)
		self.mnu.setIconSize(QtCore.QSize(96, 84)) # 96, 84
		self.mnu.setMovement(QtGui.QListView.Static)
		self.mnu.setMaximumWidth(128) # 128
		self.mnu.setSpacing(12) # 12

		# pages
		self.pages.addWidget(SettingsPage())
		self.pages.addWidget(GUIPage())
		#...

		# load icons note! after creation listview (self.mnu)
		self.initIcons()

		# select 1-st page
		self.mnu.setCurrentRow(0)

		# buttons
		bb = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
		bb.button(QtGui.QDialogButtonBox.Ok).setDefault(True)
		bb.setOrientation(QtCore.Qt.Horizontal)

		# signals
		self.connect(bb.button(QtGui.QDialogButtonBox.Ok), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('accept()'))
		self.connect(bb.button(QtGui.QDialogButtonBox.Cancel), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('reject()'))

		self.connect(self.mnu, QtCore.SIGNAL("currentItemChanged(QListWidgetItem*, QListWidgetItem*)"), self.onPage)

		# layout widgets
		hl = QtGui.QHBoxLayout()
		hl.addWidget(self.mnu)
		hl.addWidget(self.pages)

		bl = QtGui.QHBoxLayout()
		bl.addStretch(1)
		bl.addWidget(bb)

		ml = QtGui.QVBoxLayout()
		ml.addLayout(hl)
		ml.addStretch(1)
		ml.addSpacing(8) # 12
		ml.addLayout(bl)

		self.setLayout(ml)

		self.setWindowTitle(self.tr("Parameters"))

		self.setModal(True)



	def center(self):
		# cender on screen
		scr = QtGui.QDesktopWidget().availableGeometry()
		size =  self.geometry()
		self.move((scr.width()-size.width())/2,
			(scr.height()-size.height())/2)

	def initIcons(self):
		i0 = QtGui.QListWidgetItem(self.mnu)
		i0.setIcon(QtGui.QIcon(':/img/book.png'))
		i0.setText(self.tr("Scanner"))
		i0.setTextAlignment(QtCore.Qt.AlignHCenter)
		i0.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)

		i1 = QtGui.QListWidgetItem(self.mnu)
		i1.setIcon(QtGui.QIcon(':/img/app.png'))
		i1.setText(self.tr("Appearance"))
		i1.setTextAlignment(QtCore.Qt.AlignHCenter)
		i1.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)

	def onPage(self, current, previous):
		if not current: current = previous
		self.pages.setCurrentIndex(self.mnu.row(current))

	@property
	def fieldList(self):
		"""
		Returns list of tuples (QWidget*, detected data type(bool, int, str))
		based on widget.objectName():
		1. objectName() must start with '_'
		2. next sybmol in simple Polish notation:
			- 'cb' checkbox, bool
			- 'i' spinedit, int
			- 's' lineedit, string
			... other will be added as used
		3. Abbreviated field name, capitalized
		"""
		ret = []
		r = QtCore.QRegExp('^_([a-z]+)[^a-z]+.*')
		for c in self.findChildren(QtGui.QWidget, QtCore.QRegExp('^_[a-z]+[^a-z]+.*')):
			if r.indexIn(c.objectName()) != -1:
				ret.append((c, r.cap(1)))
		return ret



if __name__=="__main__":
	import doctest
	import sys, os, os.path, locale
	doctest.testmod()

	app = QtGui.QApplication(sys.argv)

	fn = "%s_%s.qm" % (os.path.splitext(__file__)[0], locale.getlocale()[0] or locale.getdefaultlocale()[0])
	if os.access(fn, os.R_OK):
		tr = QtCore.QTranslator()
		tr.load(fn)
		app.installTranslator(tr)

	dlg = SetupDialog()
	dlg.show()
	app.exec_()
